#define _CRT_SECURE_NO_WARNINGS 1
#include"SeqList.h"

void CheckSeqList(s* ps)
{
	assert(ps);
	/*检查空间容量*/
	if (ps->count == ps->capacity)
	{
		int newcapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;
		int* tmp = realloc(ps->capacity, newcapacity * sizeof(SeqListdatatype));
		if (tmp == NULL)
		{
			perror("realloc fail");
			return;
		}
		ps->pc = tmp;
		ps->capacity = newcapacity;
	}
}

void SeqListInit(s* ps)
{
	assert(ps);
	ps->pc = NULL;
	ps->capacity = 0;
	ps->count = 0;
}

void DestroySeqList(s* ps)
{
	assert(ps);
	free(ps);
	ps->pc = NULL;
	ps->capacity = 0;
	ps->count = 0;
}

void PushBck(s* ps, SeqListdatatype x)
{
	assert(ps);
	/*检查空间容量*/
	if (ps->count == ps->capacity)
	{
		int newcapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;
		SeqListdatatype* tmp=(SeqListdatatype*)realloc(ps->capacity,newcapacity*sizeof(SeqListdatatype));
		if (tmp == NULL)
		{
			perror("realloc fail");
			return;
		}
		ps->pc = tmp;
		ps->capacity = newcapacity;
	}
	ps->pc[ps->count] = x;
	ps->count++;
}

void PushFront(s* ps, SeqListdatatype x)
{
	assert(ps);
	CheckSeqList(ps);
	int end = ps->count - 1;
	while (end >= 0)
	{
		ps->pc[ps->count+1] = ps->pc[ps->count];
		end--;
	}
	ps->pc[0] = x;
	ps->count++;
}

void PopBackSeqList(s* ps)
{
	assert(ps);
	ps->count--;
}

void PrintSeqList(s* ps)
{
	assert(ps);
	int i = 0;
	for (i = 0; i < ps->count; i++)
	{
		printf("%d", ps->pc[i]);
	}
	printf("\n");
}

void PopFrontSeqList(s* ps)
{
	assert(ps);
	int end = ps->count - 1;
	while (end >= 0)
	{
		ps->pc[ps->count] = ps->pc[ps->count + 1];
		end--;
	}
	ps->count--;
	ps->capacity--;
}

int FindSeqList(s* ps,SeqListdatatype x)
{
	assert(ps);
	int i = 0;
	for (i = 0; i < ps->count; i++)
	{
		if (x == ps->pc[i])
			return i;
	}
}

void InsertSeqList(s* ps, size_t pos, SeqListdatatype x)
{
	assert(ps);
	assert(pos <= ps->count);
	CheckSeqList(ps);
	size_t end = ps->count;
	while (end > pos)
	{
		ps->pc[end] = ps->pc[end - 1];
		--end;
	}
	ps->pc[pos] = x;
	++ps->count;
}

void EraseSeqList(s* ps, size_t pos)
{
	assert(ps);
	assert(pos < ps->count);
	size_t begin = pos;
	while (begin < ps->count - 1)
	{
		ps->pc[begin] = ps->pc[begin + 1];
		++begin;
	}
	ps->count--;
}